package com.roger.ats.init.cases;

import android.os.SystemClock;
import android.provider.Settings;
import android.support.test.uiautomator.UiScrollable;
import android.support.test.uiautomator.UiSelector;

import com.roger.ats.init.util.CfgUtils;
import com.roger.ats.init.util.LogUtils;
import com.roger.uia.helper.UiBase;
import com.roger.uia.helper.manager.Account;
import com.roger.uia.helper.manager.Package;
import com.roger.uia.helper.util.StrUtils;

import static com.roger.uia.helper.UiBase.mDevice;
import static com.roger.uia.helper.UiBase.waitTime;

public class AccountInit {
    private static final String TAG = "Test" + AccountInit.class.getSimpleName();
    private static LogUtils logUtils = new LogUtils();
    private static CfgUtils cfgUtils = new CfgUtils();
    private static String email_PRC = "com.android.email";
    private static String email_ROW = "com.google.android.gm";

    public static void launchGoogle(){
        mDevice.pressHome();
        String account = cfgUtils.getCfg("google账号=");
        String pwd = cfgUtils.getCfg("google密码=");
        logUtils.printLog("==start set google email==");
        for ( int i=0;i<3;i++ ){
            try{
                if(Package.pac().isAppInstalled(email_ROW) && !Account.acc().hasAccount(account)){
                    UiBase.forceStopApp(email_ROW);
                    UiBase.openApp(email_ROW);
                    UiBase.waitClickIfObjExist(UiBase.objSelect[0],"知道了",10);
                    UiBase.waitClickObj(UiBase.objSelect[0],"添加电子邮件地址",5);
                    UiBase.waitClickObj(UiBase.objSelect[0],"Google",5);
                    UiBase.waitObjCheck(UiBase.objSelect[0],"登录",30);
                    UiBase.inputTxtToObj(UiBase.objSelect[3],"identifierId",account);
                    UiBase.waitClickObj(UiBase.objSelect[0],"下一步",5);
                    UiBase.waitObjCheck(UiBase.objSelect[0],"欢迎",30);
                    UiBase.inputTxtToObj(UiBase.objSelect[7],"android.widget.EditText",pwd);
                    UiBase.waitClickObj(UiBase.objSelect[0],"下一步",5);
                    //need verification
                    SystemClock.sleep(waitTime * 5);
                    UiBase.checkPoint("check status",Account.acc().hasAccount(account));
                    logUtils.printLog("set google email success");
                    break;
                }else{
                    logUtils.printLog("not ROW or account already exist");
                    break;
                }
            }catch (Throwable e){
                logUtils.printLog("set google email fail");
            }finally {
                mDevice.pressHome();
            }
        }

    }

    public static void launchPOP3(){
        mDevice.pressHome();
        String account = cfgUtils.getCfg("POP3_account=");
        String pwd = cfgUtils.getCfg("POP3_pwd=");
        if(!account.equals("null")){
            logUtils.printLog("==start set pop3 email==");
            for(int i=0;i<3;i++){
                try{
                    if(Package.pac().isAppInstalled(email_PRC) && !Account.acc().hasAccount(account)){
                        UiBase.forceStopApp(email_PRC);
                        UiBase.startAct(Settings.ACTION_ADD_ACCOUNT);
                        UiBase.waitClickObj(UiBase.objSelect[0],"个人(POP3)",10);
                        UiBase.waitObjCheck(UiBase.objSelect[2],"电子邮件帐户",5);
                        if(UiBase.existObj(UiBase.objSelect[4],"电子邮件地址"))
                            UiBase.inputTxtToObj(UiBase.objSelect[4],"电子邮件地址",account);
                        else  UiBase.inputTxtToObj(UiBase.objSelect[2],"电子邮件地址",account);
                        UiBase.waitClickObj(UiBase.objSelect[2],"下一步",5);
                        UiBase.waitClickIfObjExist(UiBase.objSelect[0],"个人(POP3)",5);
                        UiBase.waitObjCheck(UiBase.objSelect[2],"登录",5);
                        UiBase.inputTxtToObj(UiBase.objSelect[2],"密码",pwd);
                        UiBase.waitClickObj(UiBase.objSelect[2],"下一步",15);
                        UiBase.waitClickObj(UiBase.objSelect[2],"下一步",15);
                        UiBase.waitClickObj(UiBase.objSelect[2],"下一步",15);
                        UiBase.waitClickObj(UiBase.objSelect[2],"下一步",15);
                        UiBase.waitClickObj(UiBase.objSelect[2],"下一步",15);
                        SystemClock.sleep(waitTime * 5);
                        UiBase.checkPoint("check status",Account.acc().hasAccount(account));
                        logUtils.printLog("set pop3 email success");
                        break;
                    }else {
                        logUtils.printLog("not PRC or account already exist");
                        break;
                    }
                }catch (Throwable e){
                    logUtils.printLog("set pop3 email fail");
                }finally {
                    mDevice.pressHome();
                }
            }
        }

    }

    public static void launchIMAP(){
        mDevice.pressHome();
        String account = cfgUtils.getCfg("IMAP_account=");
        String pwd = cfgUtils.getCfg("IMAP_pwd=");
        if(!account.equals("null")){
            logUtils.printLog("==start set imap email==");
            for(int i=0;i<3;i++){
                try{
                    if(Package.pac().isAppInstalled(email_PRC) && !Account.acc().hasAccount(account)) {
                        UiBase.forceStopApp(email_PRC);
                        UiBase.startAct(Settings.ACTION_ADD_ACCOUNT);
                        UiBase.waitClickObj(UiBase.objSelect[0], "个人(IMAP)", 10);
                        UiBase.waitObjCheck(UiBase.objSelect[2], "电子邮件帐户", 5);
                        if(UiBase.existObj(UiBase.objSelect[4],"电子邮件地址"))
                            UiBase.inputTxtToObj(UiBase.objSelect[4],"电子邮件地址",account);
                        else  UiBase.inputTxtToObj(UiBase.objSelect[2],"电子邮件地址",account);
                        UiBase.waitClickObj(UiBase.objSelect[2], "下一步", 5);
                        UiBase.waitClickIfObjExist(UiBase.objSelect[0], "个人(IMAP)", 5);
                        UiBase.waitObjCheck(UiBase.objSelect[2], "登录", 5);
                        UiBase.inputTxtToObj(UiBase.objSelect[2], "密码", pwd);
                        UiBase.waitClickObj(UiBase.objSelect[2], "下一步", 15);
                        UiBase.waitClickObj(UiBase.objSelect[2], "下一步", 15);
                        UiBase.waitClickObj(UiBase.objSelect[2], "下一步", 15);
                        SystemClock.sleep(waitTime * 5);
                        UiBase.checkPoint("check status", Account.acc().hasAccount(account));
                        logUtils.printLog("set imap email success");
                        break;
                    }else {
                        logUtils.printLog("not PRC or account already exist");
                        break;
                    }
                }catch (Throwable e){
                    logUtils.printLog("set imap email fail");
                }finally {
                    mDevice.pressHome();
                }
            }
        }

    }

    public static void launchLENOVO(){
        mDevice.pressHome();
        String account = cfgUtils.getCfg("联想账号=");
        String pwd = cfgUtils.getCfg("联想密码=");
        if(!account.equals("null")){
            logUtils.printLog("==start set lenovo email==");
            for(int i=0;i<3;i++){
                try{
                    UiBase.openApp(StrUtils.settingPac);
                    UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
                    new UiScrollable(new UiSelector().resourceId(StrUtils.settingDashBoard)).scrollIntoView(UiBase.getObj(UiBase.objSelect[0],"Lenovo ID"));
                    if(!UiBase.existObj(UiBase.objSelect[1],"@lenovo.com")) {
                        UiBase.startAct(Settings.ACTION_ADD_ACCOUNT);
                        UiBase.waitClickObj(UiBase.objSelect[2], "联想帐号", 10);
                        UiBase.waitClickObj(UiBase.objSelect[2], "登录|邮箱登录", 5);
                        UiBase.waitObjCheck(UiBase.objSelect[3], "com.lenovo.lsf:id/at_account", 5);
                        UiBase.inputTxtToObj(UiBase.objSelect[3], "com.lenovo.lsf:id/at_account", account);
                        UiBase.inputTxtToObj(UiBase.objSelect[3], "com.lenovo.lsf:id/et_password", pwd);
                        UiBase.waitClickObj(UiBase.objSelect[3], "com.lenovo.lsf:id/b_login", 5);
                        SystemClock.sleep(waitTime * 5);
                        UiBase.openApp(StrUtils.settingPac);
                        UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
                        new UiScrollable(new UiSelector().resourceId(StrUtils.settingDashBoard)).scrollIntoView(UiBase.getObj(UiBase.objSelect[0],"Lenovo ID"));
                        if(!UiBase.existObj(UiBase.objSelect[0],"Lenovo ID"))
                            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[0],"用户和帐号");
                        UiBase.checkPoint("check status", UiBase.existObj(UiBase.objSelect[1],"@lenovo.com"));
                        logUtils.printLog("set lenovo email success");
                        break;
                    } else {
                        logUtils.printLog("lenovo account already exist");
                        break;
                    }
                }catch (Throwable e){
                    logUtils.printLog("set lenovo email fail");
                }finally {
                    mDevice.pressHome();
                }
            }
        }
    }

}